package cn.agiledata.bank.barcodepayment.form;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.hibernate.SessionFactory;

import cn.agiledata.bank.common.dao.HqlPageListTemplate;
import cn.agiledata.bank.common.util.Constant;
import cn.agiledata.bank.common.util.DateUtil;
import com.vnv.infrastructure.logic.IPBAppFactory;

public class PayHistoryQueryPageList extends HqlPageListTemplate {

	private BarCodePayForm form;

	public PayHistoryQueryPageList(BarCodePayForm form) {
		this.setSessionFactory((SessionFactory) IPBAppFactory.getObj("sessionFactory"));

		this.form = form;
	}

	public String getHql() {
		if (form == null) {
			String hql = "select new cn.agiledata.bank.barcodepayment.logic.PayHistoryQueryBean(bar.chargeDate, bar.updateTime,";
			hql += "  bar.barCodePayCorp.name, bar.barCode, bar.amount, bar.account, bar.payfeeStatus,bar.barCodePayCorp.barCodePayType.name," +
					"bar.payFeeNo)";
			hql += " from BarCodePayDetail bar and bar.account.id = :accountId order by bar.createTime desc";
			return hql;
		} else if (form.getBarOrMobile().equals("bar")) {
			String hql = "select new cn.agiledata.bank.barcodepayment.logic.PayHistoryQueryBean(bar.chargeDate,bar.updateTime,";
			hql += " bar.barCodePayCorp.name, bar.barCode, bar.amount, bar.account, bar.payfeeStatus,bar.barCodePayCorp.barCodePayType.name," +
					"bar.payFeeNo) from BarCodePayDetail bar where bar.account.id = :accountId";
			if (StringUtils.isNotBlank(form.getCode()))
				hql += " and bar.barCode like :barCode";
			if (StringUtils.isNotBlank(form.getStatus()))
				hql += " and bar.payfeeStatus = :status";
			if (StringUtils.isNotBlank(form.getFromAmount()))
				hql += " and bar.amount >= :fromAmount";
			if (StringUtils.isNotBlank(form.getToAmount()))
				hql += " and bar.amount <= :toAmount";
			if (StringUtils.isNotBlank(form.getStartDate()))
				hql += " and bar.updateTime >= :startDate";
			if (StringUtils.isNotBlank(form.getEndDate()))
				hql += " and bar.updateTime <= :endDate";
			hql += " order by bar.createTime desc";
			return hql;
		}else {
			String hql = " select new cn.agiledata.bank.barcodepayment.logic.PayHistoryQueryBean(a.createTime, a.updateTime,";
			hql += " a.mobileNum, a.realAmount, a.accountSeq, a.status)";
			hql += " from MobileChargeRecord a where a.accountSeq = :accountId";
			if (StringUtils.isNotBlank(form.getCode()))
				hql += " and a.mobileNum like :barCode";
			if (StringUtils.isNotBlank(form.getStatus()))
				hql += " and a.status = :status";
			if (StringUtils.isBlank(form.getStatus()))
				hql += " and a.status <> :status";
			if (StringUtils.isNotBlank(form.getFromAmount()))
				hql += " and a.realAmount >= :fromAmount";
			if (StringUtils.isNotBlank(form.getToAmount()))
				hql += " and a.realAmount <= :toAmount";
			if (StringUtils.isNotBlank(form.getStartDate()))
				hql += " and a.updateTime >= :startDate";
			if (StringUtils.isNotBlank(form.getEndDate()))
				hql += " and a.updateTime <= :endDate";
			hql += " order by a.createTime desc";
			return hql;
		}
	}

	public String getPageRecord() {
		return String.valueOf(Constant.PAGESIZE);
	}

	public Map getParams() {
		Map map = new HashMap();
		if (form == null) {
			return map;
		}
			map.put("accountId", form.getAccountId());
		if (StringUtils.isNotBlank(form.getCode())) {
			map.put("barCode", "%" + form.getCode() + "%");
		}
		if(StringUtils.isNotBlank(form.getStatus())){
			map.put("status", form.getStatus());
		}
		if (form.getBarOrMobile().equals("mobile") && StringUtils.isBlank(form.getStatus())) {
			map.put("status", Constant.MOBILE_CHARGE_STATUS_CREATE);
		}
		if (StringUtils.isNotBlank(form.getFromAmount())){
			map.put("fromAmount", Double.valueOf(form.getFromAmount()));
		}
		if (StringUtils.isNotBlank(form.getToAmount())){
			map.put("toAmount", Double.valueOf(form.getToAmount()));
		}
		if (StringUtils.isNotBlank(form.getStartDate())){
			map.put("startDate", DateUtil.string2Date(form.getStartDate() + " 00:00:00"));
		}
		if (StringUtils.isNotBlank(form.getEndDate())){
			map.put("endDate", DateUtil.string2Date(form.getEndDate() + " 23:59:59"));
		}
		return map;
	}

	public String getStartPage() {
		if (form == null) {
			return "1";
		}
		return form.getStartPage();
	}

}
